<template>
  <div>
    <h4 class="lighter">
      <i class="ace-icon fa fa-hand-o-right icon-animated-hand-pointer blue"></i>
      <router-link to="/business/course" class="pink"> {{course.name}} </router-link>
    </h4>
    <hr>
    <p>
      <router-link class="btn btn-white btn-default btn-round" to="/business/course">
        <i class="ace-icon fa fa-arrow-left"></i>
        返回课程
      </router-link>
      &nbsp;
      <button class="btn btn-white btn-default btn-round" @click="add()">
        <i class="ace-icon fa fa-edit"></i>
        新增
      </button>
      &nbsp;
      <button class="btn btn-white btn-default btn-round" @click="list(1)">
        <i class="ace-icon fa fa-refresh"></i>
        刷新
      </button>
    </p>

    <pagination ref="pagination" :list="list"></pagination>

    <table id="simple-table" class="table  table-bordered table-hover">
      <thead>
      <tr>

        <th>ID</th>
        <th>名称</th>
        <th>顺序</th>
        <th>操作</th>

      </tr>
      </thead>

      <tbody>
      <tr v-for="chapter in chapters"
          :key="chapter.id"
          :index="chapter.id">
        <td class="text-center">
          {{chapter.id}}
        </td>
        <td class="text-center">
          {{chapter.name}}
        </td>
        <td class="text-center">
          {{chapter.sort}}
        </td>

        <td>
          <div class="hidden-sm hidden-xs btn-group">
            <button class="btn btn-xs btn-info" @click="toSection(chapter)">
              小节
            </button>
            <button class="btn btn-xs btn-info" @click="edit(chapter)">
              编辑
            </button>
            <button class="btn btn-xs btn-danger" @click="del(chapter.id)">
              删除
            </button>
          </div>
        </td>
      </tr>
      </tbody>
    </table>

    <!-- Modal -->
    <div id="form-modal" class="modal fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
      <div class="modal-dialog" role="document">
        <div class="modal-content">
          <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
            <h4 class="modal-title" id="myModalLabel">大章表单</h4>
          </div>
          <div class="modal-body">
            <form class="form-horizontal">
              <div class="form-group">
                <label class="col-sm-2 control-label">名称</label>
                <div class="col-sm-10">
                  <input class="form-control" placeholder="名称" v-model="chapter.name">
                </div>
              </div>
              <div class="form-group">
                <label class="col-sm-2 control-label">顺序</label>
                <div class="col-sm-10">
                  <input class="form-control" placeholder="顺序" v-model="chapter.sort">
                </div>
              </div>
              <div class="form-group">
                <label class="col-sm-2 control-label">课程</label>
                <div class="col-sm-10">
                  <p class="form-control-static">{{course.name}}</p>
                </div>
              </div>
            </form>
          </div>
          <div class="modal-footer">
            <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
            <button type="button" class="btn btn-primary" @click="save()">保存</button>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
  import Pagination from "../../components/pagination"

  export default {
    name: "chapter",
    components: {Pagination},
    data() {
      return {
        chapter: {
          name: null,
          courseId: null,
          sort: null
        },
        chapters: [],
        course: {}
      }
    },
    mounted() {
      let _this = this
      //  因为section和chapter的路由跳转在admin.vue中被删除, 所以要在这里设置激活course的选中样式
      _this.$parent.activeSidebar('business-course-sidebar')
      //  获取在course.vue的toChapter()存的SessionStorage
      let course = SessionStorage.get(SESSION_KEY_COURSE) || {}
      if (Tool.isEmpty(course)) {
        _this.$router.push('/welcome')
      }
      _this.course = course
      _this.list(1)
    },
    methods: {
      /**
       * 获取大章（分页）
       * @param page
       */
      list(page) {
        let _this = this

        Loading.show()

        _this.$axios.post(process.env.VUE_APP_SERVER + '/business/admin/chapter/list', {
          page: page,
          size: _this.$refs.pagination.size,
          courseId: _this.course.id
        }).then((resp) => {
          if (resp.data.success) {
            _this.chapters = resp.data.content.list
            _this.$refs.pagination.render(page, resp.data.content.total)

            Loading.hide()
          } else {
            Loading.hide()
          }
        })
      },

      /**
       * 添加大章（显示模态框）
       */
      add() {
        let _this = this
        _this.chapter = {
          name: null,
          courseId: null
        }
        //  开启模态框
        $("#form-modal").modal('show')
      },

      /**
       * 编辑大章（显示模态框）
       */
      edit(chapter) {
        let _this = this
        //  Object.assign(target, source)方法用于对象的合并，将源对象（source）的所有可枚举属性，复制到目标对象（target)
        Object.assign(_this.chapter, chapter)
        //  开启模态框
        $("#form-modal").modal('show')
      },

      /**
       * 保存大章
       */
      save() {
        let _this = this

        //  保存校验
        if (!Validator.require(_this.chapter.name, '名称')) {
          //  此处return直接跳出save()方法
          return
        }

        _this.chapter.courseId = _this.course.id

        _this.$axios.post(process.env.VUE_APP_SERVER + '/business/admin/chapter/save', _this.chapter).then((resp) => {
          if (resp.data.success) {
            //  关闭模态框
            $("#form-modal").modal('hide')
            //  刷新表格
            _this.list(1)
            Toast.success('保存成功！')
          } else {
            Toast.warning(resp.data.message)
          }
        })
      },

      /**
       * 删除大章（根据id）
       * @param id
       */
      del(id) {
        let _this = this
        Confirm.show('删除大章后不可恢复，是否确认删除？', () => {
          _this.$axios.delete(process.env.VUE_APP_SERVER + '/business/admin/chapter/delete/' + id).then((resp) => {
            if (resp.data.success) {
              //  刷新表格
              _this.list(1)
              Toast.success('删除成功！')
            }
          })
        })
      },

      /**
       * 跳转到小节
       */
      toSection(chapter) {
        let _this = this
        SessionStorage.set(SESSION_KEY_CHAPTER, chapter)
        _this.$router.push('/business/section')
      },
    }
  }
</script>